package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.model.response.FileLabelResponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.base.adapter.CommonFragmentPagerAdapter;
import com.topboya.linker.ui.base.fragment.BaseFragment;
import com.topboya.linker.ui.fragment.FileClassifyFragment;
import com.topboya.linker.ui.fragment.SeeFileClassifyFragment;
import com.topboya.linker.ui.presenter.SelectFileLabelPresenter;
import com.topboya.linker.ui.view.SelectFileLabelView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/*
* 选择资料 比如ppt，视频，音频等
* */
public class xSeeFileActivity extends BaseActivity<SelectFileLabelView, SelectFileLabelPresenter> implements SelectFileLabelView {

    //     在这还要获取所有资料的结果，后面下面的fragment交互使用
    private static final String TAG = "xSelectFileActivity-";
//    private Toolbar toolbar;
    private TextView toolbar_title;
    private TextView tv_toolbar_right;
    private SlidingTabLayout tab_file_label;
    private ViewPager vp_file_classify;
    private List<FileLabelResponse.Data> fileLabelLists;
    private String productId;


    @Override
    protected int getLayoutId() {

        return R.layout.xactivity_see_file;
    }

    @Override
    protected void initView() {

//        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        tab_file_label = findViewById(R.id.tab_file_label);
        vp_file_classify = findViewById(R.id.vp_file_classify);
        tv_toolbar_right = findViewById(R.id.tv_toolbar_right);

    }

    @Override
    protected void initData() {

        toolbar_title.setText("选择资料");
        tv_toolbar_right.setVisibility(View.GONE);
        productId = getIntent().getStringExtra("productId");
//            拿到资料信息中
        LogUtils.v(TAG + "productId:"+productId);
        mPresenter.getProductFileLabelList("file_type");

    }

    @Override
    protected void initListener() {

//        toolbar.setNavigationOnClickListener(v -> finish());
        vp_file_classify.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

            @Override
            public void onPageSelected(int position) {

                LogUtils.v(TAG + "onPageSelected(int position)");

//                onPagerChangedListener.onPageSelected(position);

                if (onPagerChangedListenerList.size()>0) {

                    for (PagerChangedListener listener : onPagerChangedListenerList) {
                        listener.onPageSelected(position);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }

    @Override
    protected SelectFileLabelPresenter createPresenter() {

        return new SelectFileLabelPresenter(this);
    }


    @Override
    public void getFileLableList(List<FileLabelResponse.Data> fileLabelLists) {

        LogUtils.v(TAG+ "fileLabelLists:"+fileLabelLists.toString());
        this.fileLabelLists = fileLabelLists;
       //获取全部的资料，更新的时候用
        mPresenter.getProductFileList(productId,null);

    }

    @Override
    public void getProductFileList(List<SelectFileResponse.FileList> fileLists) {

        String[] tabeltitle = new String[fileLabelLists.size()+1];
        List<BaseFragment> fragmentList = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putString("fileLabelvalues",null);
        SeeFileClassifyFragment fileClassifyFragmentall= new SeeFileClassifyFragment();
//        fileClassifyFragmentall.setfileAllLists(fileLists);
        AppConst.FileAllList.addAll(fileLists);
//        初始化map，将所有的资料都放入到map，并初始化checked值

        fileClassifyFragmentall.setArguments(bundle);
        fragmentList.add(fileClassifyFragmentall);
        tabeltitle[0] = "全部";
        for (int i =0;i < fileLabelLists.size();i++) {

            tabeltitle[i+1] = fileLabelLists.get(i).getLabel();
            SeeFileClassifyFragment fileClassifyFragment= new SeeFileClassifyFragment();
            Bundle bundle1 = new Bundle();
            bundle1.putString("fileLabelvalues",fileLabelLists.get(i).getValue());
            fileClassifyFragment.setArguments(bundle1);
            fragmentList.add(fileClassifyFragment);
        }
        CommonFragmentPagerAdapter commonFragmentPagerAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(),fragmentList);
        vp_file_classify.setAdapter(commonFragmentPagerAdapter);
        tab_file_label.setViewPager(vp_file_classify,tabeltitle);


    }

    List<PagerChangedListener> onPagerChangedListenerList = new ArrayList<>();
    public void setonPagerChangedListener(PagerChangedListener onPagerChangedListener){

        if (!onPagerChangedListenerList.contains(onPagerChangedListener)) {
            onPagerChangedListenerList.add(onPagerChangedListener);
        }
//        this.onPagerChangedListener = onPagerChangedListener;
    }
    public interface  PagerChangedListener{

        public void onPageSelected(int position);
    }

    @Override
    public void onBackPressed() {

        keyBackListener.onKeyDown();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 是否触发按键为back键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else { // 如果不是back键正常响应
            return super.onKeyDown(keyCode, event);
        }
    }

    public KeyBackListener keyBackListener;

    public void setKeyBackListener(KeyBackListener keyBackListener) {

        this.keyBackListener = keyBackListener;
    }

   public interface  KeyBackListener{

         void onKeyDown();
    }


    //  从查看资料选择页面返回来的，在查看资料页面有可能删除数据，所以选中的需要更新
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode ==1002){


            if (adapterNotifyListenerList.size()>0) {

                for (AdapterNotifyListener listener : adapterNotifyListenerList) {
                    listener.notifyDataSetChanged();
                }
            }
        }
    }

    List<AdapterNotifyListener> adapterNotifyListenerList = new ArrayList<>();
    public void setAdapterNotifyListener(AdapterNotifyListener adapterNotifyListener){

        if (!adapterNotifyListenerList.contains(adapterNotifyListener)) {
            adapterNotifyListenerList.add(adapterNotifyListener);
        }
    }
    private AdapterNotifyListener adapterNotifyListener;
    public interface  AdapterNotifyListener{

         public void notifyDataSetChanged();
    }
}


